﻿using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;
using Business;

public partial class Controls_BackEnd_ProductList : UserControl
{
    private const string ControlEdit = "productedit.ascx";



    /// <summary>
    /// 
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack) return;
        BindCategory();
        BinderData(string.Empty);
    }


    private void BindCategory()
    {
        var list = new ProductCategories().ProductCategories_GetByCondition(string.Empty);
        ddlCategory.Items.Add(new ListItem { Value = @"0", Text = @"--- Chọn category --- " });
        foreach (var productCategoriese in list)
            ddlCategory.Items.Add(new ListItem{Value=productCategoriese.CateId.ToString(),Text=productCategoriese.CateName});
        
    }


    /// <summary>
    /// 
    /// 
    /// </summary>
    /// <param name="condition"></param>
    private void BinderData(string condition)
    {
        try
        {
            Session[SessionManager.SessionList] = new Products().Products_GetByCondition(condition);
            var list = Config.ProcessPage((List<Products>)Session[SessionManager.SessionList]);
            if (list.Count > 0)
            {
                CheckPermission();
                return;
            }
            Config.NullData(new Products(), grvData);
            CheckPermission();
        }
        catch (Exception exception)
        {
            Config.NullData(new Products(), grvData);
            Config.Message(Page, Resources.Resource.ErrorMsg, Resources.Resource.Warning);
        }
    }



    /// <summary>
    /// 
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void grvData_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        switch (e.Row.RowType)
        {
            case DataControlRowType.Header:
                break;
            case DataControlRowType.DataRow:
                e.Row.Attributes.Add("class", "alternate-row");
                break;
            case DataControlRowType.Footer:
                break;
        }
    }


    private string GetCondition()
    {
        var condition = string.Empty;
        if (ddlCategory.SelectedValue != "0")
            condition += " CateID = " + ddlCategory.SelectedValue;
        if (!string.IsNullOrEmpty(txtContent.Text.Trim()))
        {
            condition = !string.IsNullOrEmpty(condition) ? condition + " and" : condition;
            if (ddlType.SelectedValue.ToLower() == "id")
            {
                condition = " CateId = " + txtContent.Text;
            }
            if (ddlType.SelectedValue.ToLower() == "name")
            {
                condition = " CateName like '%" + txtContent.Text + "%'";
            }
        }
        return condition;
    }




    /// <summary>
    /// 
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void grvData_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        switch (e.CommandName.ToLower())
        {
            case "edit":
                Config.Redirect(new Functions().GetFunctionID(ControlEdit), int.Parse(e.CommandArgument.ToString()));
                break;
            case "add":
                Config.Redirect(new Functions().GetFunctionID(ControlEdit), 0);
                break;
        }
    }




    /// <summary>
    /// 
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void AllCommand(object sender, EventArgs e)
    {
        switch (((Control)sender).ID)
        {
            case "btndel":
                try
                {
                    var countdel = 0;
                    for (var index = 0; index < grvData.Rows.Count; index++)
                    {
                        if (!(grvData.Rows[index].FindControl("cbCheck") as CheckBox).Checked) continue;
                        var isSuccess =
                            new Products { ProductID = int.Parse(grvData.DataKeys[index].Values[0].ToString()) }.Delete();
                        countdel = isSuccess >= 0 ? countdel + 1 : countdel;
                    }
                    if (countdel > 0)
                        BinderData(GetCondition());
                    Config.Message(Page, string.Format(Resources.Resource.DeleteSuccess, countdel, Resources.Resource.Category.ToLower()), Resources.Resource.Messenger);
                }
                catch (Exception ex)
                {

                }
                break;
            case "lbtnAdd":
                Config.Redirect(new Functions().GetFunctionID(ControlEdit), 0);
                break;
            case "btnSearch":
                BinderData(GetCondition());
                break;
            case "lbtnFirstPage":
                Config.SetCurrentPage((List<Products>)Session[SessionManager.SessionList], 1, Config.ChangeType.FirstPage);
                break;
            case "lbtnPreviewPage":
                Config.SetCurrentPage((List<Products>)Session[SessionManager.SessionList], int.Parse(((LinkButton)sender).CommandName) - 1, Config.ChangeType.PreviewPage);
                break;
            case "lbtnNextPage":
                Config.SetCurrentPage((List<Products>)Session[SessionManager.SessionList], int.Parse(((LinkButton)sender).CommandName) + 1, Config.ChangeType.NextPage);
                break;
            case "lbtnLastPage":
                Config.SetCurrentPage((List<Products>)Session[SessionManager.SessionList], int.Parse(hdfCount.Value), Config.ChangeType.LastPage);
                break;
            case "input":
                grvData.DataSource = Config.SetCurrentPage((List<Products>)Session[SessionManager.SessionList], int.Parse(hdfPage.Value), Config.ChangeType.ChangePage);
                grvData.DataBind();
                CheckPermission();
                break;
        }

    }





    private void CheckPermission()
    {
        var permission = (UserFunctions)Session[SessionManager.SessionPermission];
        grvData.Columns[grvData.Columns.Count - 1].Visible = permission.IsDelete;
    }
    protected void grvData_RowEditing(object sender, GridViewEditEventArgs e)
    {

    }

}


